package com.lemon.funplus.parse;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.lemon.funplus.object.AccountObject;
import com.lemon.funplus.object.DetailsCateObject;
import com.lemon.funplus.object.HomeObject;

public class JsonParser {
	public static String getJson(String url) {
		Log.e("getJson", "url: " + url);
		InputStream is = null;
		String result = "";
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result = sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return result;

	}
	public static String changePass(String phone,String oldpass,String newpass,String renewpass) {
		String body = String.format(CMD.ACCOUNT_CHANGE_PASS, phone,oldpass,newpass,renewpass);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}
	
	public static String forgetPass(String phone) {
		String body = String.format(CMD.ACCOUNT_FORGET_PASS, phone);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}

	public static String catesidebar() {
		String url = String.format(CMD.URL_MAIN, CMD.CATE_SIDE_BAR);
		return getJson(url);
	}
	
	public static String detailsItem(String cate_id) {
		String body = String.format(CMD.DETAILS_ITEM, cate_id);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}
	
	public static String catechild(String cate_id, String page, String limit) {
		String body = String.format(CMD.CATE_LIST_ITEM, cate_id, page, limit);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}
	
	public static String home(String key, String page, String limit) {
		String body = String.format(CMD.HOME, key, page, limit);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}

	public static String register(String username, String password, String fullname, String email, String phone, String gender) {
		String body = String.format(CMD.ACCOUNT_REGISTER, username, password, fullname, email, phone, gender);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}

	public static String login(String username, String password) {
		String body = String.format(CMD.ACCOUNT_LOGIN, username, password);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}

	public static String active(String phone, String activecode) {
		String body = String.format(CMD.ACCOUNT_ACTIVE, phone, activecode);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}

	public static String getActive(String phone) {
		String body = String.format(CMD.ACCOUNT_GET_ACTIVE_CODE, phone);
		String url = String.format(CMD.URL_MAIN, body);
		return getJson(url);
	}

	public static ArrayList<String> readListdata(String result) throws JSONException {
		JSONObject jsonObj = new JSONObject(result).getJSONObject("data");
		ArrayList<String> listString = new ArrayList<String>();
		if (!jsonObj.isNull("status"))
			listString.add(jsonObj.getString("status"));
		if (!jsonObj.isNull("mss"))
			listString.add(jsonObj.getString("mss"));
		return listString;
	}

	public static ArrayList<HomeObject> readHome(String result) throws JSONException {
		JSONObject jsonObject = new JSONObject(result);
		ArrayList<HomeObject> listHome = new ArrayList<HomeObject>();
		JSONArray array = jsonObject.getJSONArray("data");
		for (int i = 0; i < array.length(); i++) {
			JSONObject jsonObj = array.getJSONObject(i);
			HomeObject homeObject = new HomeObject();
			if (!jsonObj.isNull("at_id"))
				homeObject.setid(jsonObj.getString("at_id"));
			
			if (!jsonObj.isNull("at_title"))
				homeObject.settitle(jsonObj.getString("at_title"));
			if (!jsonObj.isNull("at_primary_content"))
				homeObject.setcontent(jsonObj.getString("at_primary_content"));
			if (!jsonObj.isNull("at_view"))
				homeObject.setview(jsonObj.getString("at_view"));
			if (!jsonObj.isNull("at_like"))
				homeObject.setlike(jsonObj.getString("at_like"));
			if (!jsonObj.isNull("at_dislike"))
				homeObject.setdislike(jsonObj.getString("at_dislike"));
			if (!jsonObj.isNull("at_file_type"))
				homeObject.settype(jsonObj.getString("at_file_type"));
			if (!jsonObj.isNull("at_datecreate"))
				homeObject.setcreated(jsonObj.getString("at_datecreate"));
			if (!jsonObj.isNull("at_avatar"))
				homeObject.setavatar(jsonObj.getString("at_avatar"));
			if (!jsonObj.isNull("at_moretitle"))
				homeObject.setmoretitle(jsonObj.getString("at_moretitle"));
			if (!jsonObj.isNull("at_comment"))
				homeObject.setcomment(jsonObj.getString("at_comment"));
			if (!jsonObj.isNull("link_wap"))
				homeObject.setlink_wap(jsonObj.getString("link_wap"));
			if (!jsonObj.isNull("fbcmlink"))
				homeObject.setfb_link(jsonObj.getString("fbcmlink"));
			if (!jsonObj.isNull("islike"))
				homeObject.setislike(jsonObj.getBoolean("islike"));
			if (!jsonObj.isNull("isdislike"))
				homeObject.setisdislike(jsonObj.getBoolean("isdislike"));
//			homeObject.showinfo();
			listHome.add(homeObject);
		}
		return listHome;
	}
	public static AccountObject readLogin(String result) throws JSONException {
		JSONObject jsonObj = new JSONObject(result).getJSONObject("data");
		AccountObject accountObject = new AccountObject();
		if (!jsonObj.isNull("uid"))
			accountObject.setid(jsonObj.getString("uid"));
		if (!jsonObj.isNull("uname"))
			accountObject.setname(jsonObj.getString("uname"));
		if (!jsonObj.isNull("ufirstname"))
			accountObject.setfirst_name(jsonObj.getString("ufirstname"));
		if (!jsonObj.isNull("uemail"))
			accountObject.setemail(jsonObj.getString("uemail"));
		if (!jsonObj.isNull("uphone"))
			accountObject.setphone(jsonObj.getString("uphone"));
		if (!jsonObj.isNull("ugender"))
			accountObject.setgender(jsonObj.getString("ugender"));
		if (!jsonObj.isNull("ubirthday"))
			accountObject.setbirthday(jsonObj.getString("ubirthday"));
		if (!jsonObj.isNull("ujoined_date"))
			accountObject.setjoined(jsonObj.getString("ujoined_date"));
		if (!jsonObj.isNull("ulastlogin"))
			accountObject.setlastlogin(jsonObj.getString("ulastlogin"));
		if (!jsonObj.isNull("u_avatar"))
			accountObject.setavatar(jsonObj.getString("u_avatar"));
		if (!jsonObj.isNull("u_status"))
			accountObject.setstatus(jsonObj.getString("u_status"));
		accountObject.showinfo();
		return accountObject;
	}
	
	public static DetailsCateObject readDetails(String result) throws JSONException {
		JSONObject jsonObj = new JSONObject(result).getJSONObject("data");
		DetailsCateObject detailsObject = new DetailsCateObject();
		if (!jsonObj.isNull("at_id"))
			detailsObject.setid(jsonObj.getString("at_id"));
		if (!jsonObj.isNull("at_title"))
			detailsObject.settitle(jsonObj.getString("at_title"));
		if (!jsonObj.isNull("at_fk_content"))
			detailsObject.setcontent(jsonObj.getString("at_fk_content"));
		if (!jsonObj.isNull("fbcmlink"))
			detailsObject.setfbcomment(jsonObj.getString("fbcmlink"));
		detailsObject.showinfo();
		return detailsObject;
	}
	
	public static ArrayList<String> readStatus(String result) throws JSONException {
		ArrayList<String> statusResult=new ArrayList<String>();
		JSONObject jsonObj = new JSONObject(result).getJSONObject("data");
		if (!jsonObj.isNull("status"))
			statusResult.add(jsonObj.getString("status"));
		if (!jsonObj.isNull("mss"))
			statusResult.add(jsonObj.getString("mss"));
		return statusResult;
	}
}
